#include <iostream>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/Priority.hh>

using std::cout;
using std::endl;
using namespace log4cpp;

void test0(){
    //设置日志布局
    PatternLayout* ptn1 = new PatternLayout();
    ptn1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout* ptn2 = new PatternLayout();
    ptn2->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout* ptn3 = new PatternLayout();
    ptn3->setConversionPattern("%d %c [%p] %m%n");

    //创建目的地对象
    OstreamAppender* pos = new OstreamAppender("console",&cout);
    pos -> setLayout(ptn1);

    auto pfile = new FileAppender("fileApp","ch.log");
    pfile -> setLayout(ptn2);

    auto rollingFileApp  = new RollingFileAppender("rollingFile","rollingch.log",5*1024,9);
    rollingFileApp -> setLayout(ptn3);

    //创建日志记录器
    Category & salesDepart = Category::getInstance("salesDepart");
    //设置日志系统优先级
    salesDepart.setPriority(Priority::DEBUG);
    //设置日志目的地
    salesDepart.addAppender(pos);
    salesDepart.addAppender(pfile);
    salesDepart.addAppender(rollingFileApp);

    //记录日志
    int count = 100;
    while(count-- >0){
        salesDepart.emerg("this is an emerg msg");
        salesDepart.fatal("this is a fatal msg");
        salesDepart.alert("this is an alert msg");
        salesDepart.crit("this is a crit msg");
        salesDepart.error("this is an error msg");
        salesDepart.warn("this is a warn msg");
        salesDepart.notice("this is a notice msg");
        salesDepart.info("this is an info msg");
        salesDepart.debug("this is a debug msg");
    }

    //回收资源
    Category::shutdown();


}

int main(int argc, char * argv[])
{
    test0();
    return 0;
}


